from rest_framework import serializers
from .models import ProductCategory, Product, ProductPrice, ProductPerformance

class ProductCategorySerializer(serializers.ModelSerializer):
    """产品分类序列化器"""
    class Meta:
        model = ProductCategory
        fields = '__all__'

class ProductSerializer(serializers.ModelSerializer):
    """产品序列化器"""
    category_name = serializers.CharField(source='category.name', read_only=True)
    current_price = serializers.SerializerMethodField()
    latest_performance = serializers.SerializerMethodField()
    
    class Meta:
        model = Product
        fields = '__all__'
    
    def get_current_price(self, obj):
        """获取当前价格"""
        latest_price = obj.productprice_set.order_by('-price_date').first()
        if latest_price:
            return {
                'price': latest_price.price,
                'price_date': latest_price.price_date,
                'change_amount': latest_price.change_amount,
                'change_rate': latest_price.change_rate
            }
        return None
    
    def get_latest_performance(self, obj):
        """获取最新业绩"""
        latest_performance = obj.productperformance_set.order_by('-performance_date').first()
        if latest_performance:
            return {
                'daily_return': latest_performance.daily_return,
                'weekly_return': latest_performance.weekly_return,
                'monthly_return': latest_performance.monthly_return,
                'quarterly_return': latest_performance.quarterly_return,
                'yearly_return': latest_performance.yearly_return,
                'performance_date': latest_performance.performance_date
            }
        return None

class ProductPriceSerializer(serializers.ModelSerializer):
    """产品价格序列化器"""
    product_name = serializers.CharField(source='product.name', read_only=True)
    
    class Meta:
        model = ProductPrice
        fields = '__all__'

class ProductPerformanceSerializer(serializers.ModelSerializer):
    """产品业绩序列化器"""
    product_name = serializers.CharField(source='product.name', read_only=True)
    
    class Meta:
        model = ProductPerformance
        fields = '__all__'

class ProductListSerializer(serializers.ModelSerializer):
    """产品列表序列化器（简化版）"""
    category_name = serializers.CharField(source='category.name', read_only=True)
    current_price = serializers.DecimalField(max_digits=10, decimal_places=4, read_only=True)
    daily_return = serializers.DecimalField(max_digits=8, decimal_places=4, read_only=True)
    
    class Meta:
        model = Product
        fields = ('id', 'code', 'name', 'category_name', 'risk_level', 
                 'current_price', 'daily_return', 'status')

class ProductDetailSerializer(serializers.ModelSerializer):
    """产品详情序列化器"""
    category = ProductCategorySerializer(read_only=True)
    price_history = ProductPriceSerializer(source='productprice_set', many=True, read_only=True)
    performance_history = ProductPerformanceSerializer(source='productperformance_set', many=True, read_only=True)
    
    class Meta:
        model = Product
        fields = '__all__'